package com.wjh.anyview.websocket.util;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wjh.anyview.websocket.entity.bo.TopicMessage;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import java.util.Base64;

/**
 * @author wjh
 */
public class SecurityUtil {
    /**
     * 偏移变量，固定占8位字节
     */
    private final static String IV_PARAMETER = "26913977";
    /**
     * 加密算法
     */
    private static final String ALGORITHM = "DES";
    /**
     * 加密/解密算法-工作模式-填充模式
     */
    private static final String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding";
    /**
     * 默认编码
     */
    private static final String CHARSET = "utf-8";
    /**
     * 秘钥
     */
    private static final String SECURITY_KEY = "Wjhab_666";

    /**
     * 加密生成的TopicMessage
     *
     * @param topicMessage 未加密的原生topicMessage
     * @return 返回token
     */
    public static String getTopicMessage(TopicMessage topicMessage) {
        String token = null;
        try {
            token = new ObjectMapper().writeValueAsString(topicMessage);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return SecurityUtil.encrypt( token);
    }

    /**
     * 解密加密后的TopicMessage
     *
     * @param topicMessage 加密的topicMessage
     * @return 返回TokenBo对象
     */
    public static TopicMessage decodeTopicMessage(String topicMessage) {
        return JSON.parseObject(SecurityUtil.decrypt( topicMessage)).toJavaObject(TopicMessage.class);
    }


    /**
     * 生成key
     *
     * @return Key
     * @throws Exception 抛出异常
     */
    private static Key generateKey() throws Exception {
        DESKeySpec dks = new DESKeySpec(SecurityUtil.SECURITY_KEY.getBytes(CHARSET));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        return keyFactory.generateSecret(dks);
    }


    /**
     * DES加密字符串
     *
     * @return 加密后内容
     */
    public static String encrypt( String data) {

        if (data == null) {
            return null;
        }
        try {
            Key secretKey = generateKey();
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET));
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
            // 加密
            byte[] bytes = cipher.doFinal(data.getBytes(CHARSET));
            // base64编码  JDK1.8及以上可直接使用Base64，JDK1.7及以下可以使用BASE64Encoder
            byte[] encode = Base64.getEncoder().encode(bytes);
            return new String(encode);
        } catch (Exception e) {
            e.printStackTrace();
            return data;
        }
    }

    /**
     * DES解密字符串
     *
     * @param data       待解密字符串
     * @return 解密后内容
     */
    public static String decrypt( String data) {
        if(data==null){
            return null;
        }
        try {
            Key secretKey = generateKey();
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET));
            cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
            // base64解码
            byte[] decode = Base64.getDecoder().decode(data.getBytes(CHARSET));
            // 解密
            byte[] decrypt = cipher.doFinal(decode);
            return new String(decrypt, CHARSET);
        } catch (Exception e) {
            e.printStackTrace();
            return data;
        }
    }


}
